package com.tjl.yangxixis.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ToastUtils;
import com.lyp.jsonbean.LoginBean;
import com.lyp.jsonbean.UpdateBean;
import com.tjl.yangxixis.R;
import com.tjl.yangxixis.api.YangxixiApi;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 *
 * @author Administrator
 *  修改密码(销售经理,销售员)的界面
 *
 */
public class UpdatePwdActivity extends Activity implements OnClickListener{

	private Button mConfirm;
	private ImageView mBack;
	private TextView mName;
	private EditText mOldPwd,mNewPwd,mConfirmPwd;
	private LoginBean.DataBean bean;
	private String mOldpwds,mNewPwds,mConfirmPwds;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_pwd);
		initview();
	}

	public void initview(){
		bean = (LoginBean.DataBean) getIntent().getExtras().get("databean");
		mBack = (ImageView) findViewById(R.id.iv_alter_back);
		mName = (TextView) findViewById(R.id.tv_update_name);
		mOldPwd = (EditText) findViewById(R.id.et_old_pwd);
		mNewPwd = (EditText) findViewById(R.id.et_new_pwd);
		mConfirmPwd = (EditText) findViewById(R.id.et_confirm_pwd);
		mConfirm = (Button) findViewById(R.id.btn_order_confirm);
		mBack.setOnClickListener(this);
		mName.setText(bean.getUser());
		mConfirm.setVisibility(View.VISIBLE);
		mConfirm.setOnClickListener(this);
	}


	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
			case R.id.iv_alter_back:
				finish();//返回上个界面
				break;
			case R.id.btn_order_confirm:
				if (check()) {
					updatepwd(bean.getUser(),bean.getPwd(),mNewPwds);
				}
				break;
			}
	}

	//修改密码
	public void updatepwd(String user,String old_pwd,String new_pwd) {
		Retrofit retrofit = new Retrofit.Builder()
				.baseUrl(YangxixiApi.APP_URL)
				.addConverterFactory(GsonConverterFactory.create())
				.build();
		YangxixiApi api = retrofit.create(YangxixiApi.class);
		Call<UpdateBean> call = api.getUpdate(user,old_pwd,new_pwd);
		call.enqueue(new Callback<UpdateBean>() {
			@Override
			public void onResponse(Call<UpdateBean> call, final Response<UpdateBean> response) {
				if (response.body().getResult() == 1 ) {
					UpdatePwdActivity.this.runOnUiThread(new Runnable() {
						@Override
						public void run() {
							new MaterialDialog.Builder(UpdatePwdActivity.this)
									.title("密码修改成功")
									.content("请妥善保存新密码")
									.positiveText("我知道了")
									.canceledOnTouchOutside(false)
									.cancelable(false)
									.onPositive(new MaterialDialog.SingleButtonCallback(){
										@Override
										public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
											finish();
										}
									})
									.show();
								}
						});
				}
				else if (response.body().getResult() == 0){
					ToastUtils.showShort(response.body().getMessage());
				}
			}
			@Override
			public void onFailure(Call<UpdateBean> call, Throwable t) {
				ToastUtils.showShortSafe(t.getMessage());
			}
		});
	}

	//判断密码是否正确或为空
	public boolean check() {
		mOldpwds = mOldPwd.getText().toString();
		mNewPwds = mNewPwd.getText().toString();
		mConfirmPwds = mConfirmPwd.getText().toString();
		if (mOldpwds.isEmpty()) {
			ToastUtils.showShort("原密码为空");
			return false;
		}else if (!mOldpwds.equals(bean.getPwd())){
			ToastUtils.showShort("原密码输入错误");
			return false;
		}else if (mNewPwds.isEmpty()) {
			ToastUtils.showShort("新密码为空");
			return false;
		} else if (mOldpwds.equals(mNewPwds)) {
			ToastUtils.showShort("新密码不能等于旧密码");
			return false;
		} else if (!mNewPwds.equals(mConfirmPwds)) {
			ToastUtils.showShort("两次密码输入不相等");
			return false;
		}
		return true;
	}
}
